/*
 * @Author: HuRrongChao 1556810922@qq.com
 * @Date: 2024-10-08 17:02:40
 * @LastEditors: HuRrongChao
 * @LastEditTime: 2025-04-18 17:01:37
 */

import api from '@/api/axios.js'
import api_functions from '@/api/functions'
import { ElMessage } from 'element-plus'

// 推荐视频数据相关配置
export default {
  namespaced: true,
  // 准备actions--用于响应组件中的动作
  actions: {
    async add_type_num(context, [type, classList]) {
      if (type === 'love') {
        context.commit('ADD_TYPE_NUM', ['loved', 'love_num', 'loveStatus', classList])
        api.toggleLike({
          video_id: context.state.videoInfoList[context.state.nowListIndex].video_id
        })
      }
      else if (type === 'comment') {
        context.commit('CHANGE_STATUS', 'commentStatus')
      }
      else if (type === 'collect') {
        context.commit('ADD_TYPE_NUM', ['collected', 'collect_num', 'collectStatus', classList])
        api.toggleCollection({
          video_id: context.state.videoInfoList[context.state.nowListIndex].video_id
        })
      }
      else if (type === 'forward') {
        try {
          api.shareVideo({
            video_id: context.state.videoInfoList[context.state.nowListIndex].video_id
          })
          await api_functions.copyToClipboard(context.state.videoInfoList[context.state.nowListIndex].video_url)
          context.state.videoInfoList[context.state.nowListIndex].forward_num++
          ElMessage({
            message: '已成功复制到粘贴板!',
            type: 'success'
          })
        } catch (error) {
          ElMessage({
            message: '复制失败，请重试。',
            type: 'error'
          })
        }
      }
    }
  },
  // mutations--用于操作数据state
  mutations: {
    initVideoListInfo(state, value) {
      state.videoInfoList = value
      for (let index = 0; index < state.videoInfoList.length; index++) {
        const element = state.videoInfoList[index];
        element.loveStatus = false
        element.commentStatus = false
        element.collectStatus = false
        element.forwardStatus = false
      }
    },
    initVideoListIndex(state, value) {
      state.nowListIndex = value
    },
    initVideoCurComment(state, value) {
      state.nowCommentInfo = value
    },
    ADD_TYPE_NUM(state, [classType, numType, statusType, classList]) {
      let nowStatus = state.videoInfoList[state.nowListIndex][statusType]
      if (!nowStatus) {
        state.videoInfoList[state.nowListIndex][numType]++
        classList.add(classType)
      }
      else {
        state.videoInfoList[state.nowListIndex][numType]--
        classList.remove(classType)
      }
      state.videoInfoList[state.nowListIndex][statusType] = !nowStatus
    },
    CHANGE_STATUS(state, statusType) {
      state[statusType] = !state[statusType]
    }
  },
  // 准备state--用于存储数据
  state: {
    nowListIndex: 0,
    videoInfoList: [],
    nowCommentInfo: [],
    commentStatus: false,
  },
  getters: {
    isFollowed: (state) => (index) => {
      return state.videoInfoList[index] ? state.videoInfoList[index].isFollowed : false
    },
    author_id: (state) => (index) => {
      return state.videoInfoList[index] ? state.videoInfoList[index].author_id : false
    },
    avatar_url: (state) => (index) => {
      return state.videoInfoList[index] ? state.videoInfoList[index].avatar_url : ''
    },
    loveNum: (state) => (index) => {
      return state.videoInfoList[index] ? state.videoInfoList[index].love_num : 0
    },
    loveStatus: (state) => (index) => {
      return state.videoInfoList[index] ? state.videoInfoList[index].loveStatus : false
    },
    commentNum: (state) => (index) => {
      return state.videoInfoList[index] ? state.videoInfoList[index].comment_num : 0
    },
    commentStatus: (state) => (index) => {
      return false
    },
    collectNum: (state) => (index) => {
      return state.videoInfoList[index] ? state.videoInfoList[index].collect_num : 0
    },
    collectStatus: (state) => (index) => {
      return state.videoInfoList[index] ? state.videoInfoList[index].collectStatus : false
    },
    forwardNum: (state) => (index) => {
      return state.videoInfoList[index] ? state.videoInfoList[index].forward_num : 0
    },
    forwardStatus: (state) => (index) => {
      return false
    },
  }
}